//
//  SPHomeViewController.swift
//  Swift-demo
//
//  Created by zcp on 2021/9/27.
//

import UIKit

class SPHomeViewController: SPBaseViewController {
    
    private var modules = [Modules]()
    
    private lazy var titleArray: Array = {
        return [["icon":"sep_register", "title": "签到"],
                ["icon":"sep_mywallet", "title": "钱包"],
                ["icon":"sep_subscription", "title": "订阅"],
                ["icon":"sep_fengyintu", "title": "封印图"],
                ["icon":"sep_theme", "title": "皮肤"],
                ["icon":"myvotebiao", "title": "我的投票"],
                ["icon":"sep_help", "title": "帮助反馈"],
                ["icon":"sep_beijing", "title": "首都网警"],
                ["icon":"sep_auther", "title": "作者中心"],
                ["icon":"sep_setting", "title": "设置"]]
    }()
    
    private lazy var collectionView: UICollectionView = {
        let fl = UICollectionViewFlowLayout()
        fl.minimumInteritemSpacing = 5
        fl.minimumLineSpacing = 10
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: fl)
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.alwaysBounceVertical = true
        collectionView.register(supplementaryViewType: SPHomeHeaderView.self, ofKind: UICollectionView.elementKindSectionHeader)
        collectionView.register(supplementaryViewType: SPHomeFooterView.self, ofKind: UICollectionView.elementKindSectionFooter)
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
//        collectionView.rHead = MJRefreshHeader
//        collectionView.rHead = SPRefreshGifHeader(refreshingBlock: {
//            print("MJRefreshNormalHeader")
//        })
//        collectionView.rFoot = SPRefreshTipKissFooter(refreshingBlock: {
//            print("MJRefreshAutoFooter")
//        })
        ApiProvider.getRequest(Api.findHome, model: HomeDatasModel.self) { [weak self] (returnData) in
            print(returnData)
            self?.modules = returnData?.modules ?? []
            self?.collectionView.reloadData()
        }
        
    }
    
    func initUI() {
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension SPHomeViewController: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        let model = modules[section]
        return model.items?.count ?? 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = SPHomeCollectionViewCell.dequeueReusabCell(collectionView: collectionView, indexPath: indexPath as NSIndexPath, cellType: SPHomeCollectionViewCell.self)
//        cell.setData(dict: titleArray[indexPath.row], indexPath: nil)
        cell.model = modules[indexPath.section].items?[indexPath.row].first
        return cell
        
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.modules.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = modules[indexPath.section]
        switch model.items?.count {
        case 4:
            let width = floor(Double(kScreenWidth - 30)/2)
            return CGSize(width: width, height: width * 0.85)
        case 5:
            if indexPath.row == 0 {
                let width = floor(Double(kScreenWidth - 30)/3)
                return CGSize(width: width * 2, height: width * 1.75)
            }
            let width = floor(Double(kScreenWidth - 30)/3)
            return CGSize(width: width, height: width * 1.75)
        default:
            let width = floor(Double(kScreenWidth - 30)/3)
            return CGSize(width: width, height: width * 1.75)
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    
    // 返回HeadView的宽高
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        return CGSize(width: kScreenWidth, height: 50)
    }


    // 返回自定义HeadView或者FootView，我这里以headview为例
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
    {
        //            var reusableview:UICollectionReusableView!

        if kind == UICollectionView.elementKindSectionHeader
        {
            let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, for: indexPath, viewType: SPHomeHeaderView.self)
            let model = modules[indexPath.section]
            reusableview.imageV.kf.setImage(with: URL(string: model.moduleInfo?.icon ?? ""))
            reusableview.titleL.text = model.moduleInfo?.title
            reusableview.moreBtnAction { dict in
                print(dict)
            }
            reusableview.delegate = self
            return reusableview
        }else{
            let resuableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, for: indexPath, viewType: SPHomeFooterView.self)
            return resuableView
        }
    }
}

extension SPHomeViewController: homeHeaderViewDelegate{
    func homeHeaderViewMoreBtnAction(_ homeHeaderView: SPHomeHeaderView, param: [String : Any]) {
        print(param)
    }
}
